package net.sureon.common.mail;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * Default implemention  of the MailListener interface, it just logs the
 * start, sent and stop messages
 */
public class DefaultMailListener implements MailListener{
    private static final Log LOGGER = LogFactory.getLog(DefaultMailListener.class);


    /**
     * Called when an Mail Processor starts processing the Mails.
     * This method is only called once after invoking
     * MailProcessor.processMail()
     *
     * @param key     The Key that identifies the task
     * @param from    The email address of the sender
     */
    public void start(Object key, String from) {
        if(LOGGER.isInfoEnabled()){
            LOGGER.info("Started Processing mail from " + from + " [Key: " + key.toString() + "]");
        }
    }
    /**
     * Called when an email is successfully sent to the
     * Mail server
     *
     * @param key     The Key that identifies the task
     * @param to  The email address of the recipient
     */
    public void sendSuccessful(Object key, String to) {
        if(LOGGER.isInfoEnabled()){
            LOGGER.info("Successfully sent to " + to + " [Key: " + key.toString() + "]");
        }
    }

    /**
     * Called when an email failed to be sent to the
     * Mail server
     *
     * @param key     The Key that identifies the task
     * @param to        The email address of the recipient
     * @param exception The Exception for the failure.
     */
    public void sendFailed(Object key, String to, Exception exception) {
        if(LOGGER.isInfoEnabled()){
            LOGGER.info("Failed to sent mail " + to + " [Key: " + key.toString() + "] Exception - "  + exception.getMessage());
        }
    }

    /**
     * Called when an Mail Processor finishes processing the Mails.
     * This method is only called once at the end of
     * MailProcessor.processMail()
     *
     * @param key     The Key that identifies the task
     */
    public void done(Object key) {
        if(LOGGER.isInfoEnabled()){
            LOGGER.info("Mail Processing done " + " [Key: " + key.toString() + "]");
        }
    }






}
